package de.seminar.lego3D.fragments;

import java.util.regex.Matcher;

import de.seminar.lego3D.Constants;
import de.seminar.lego3D.Lego3DActivity;
import de.seminar.lego3D.R;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

/**
 * Fragment, that gives the user the possibility to type in the IP address of the server.
 * @author Christian Lander
 *
 */
public class SettingsFragment extends DetailsFragment {

	/** context of this view **/
	private Context ctx;

	/** edit text for IP **/
	private EditText editText;

	/** connect button **/
	private Button connectBtn;

	/** disconnect button **/
	private Button disconnectBtn;

	/** image that states the connection state **/
	private ImageView connImg;

	/** image that holds the animation for the mechanic **/
	private ImageView mechanicImg;

	/** the animation drawables **/
	private AnimationDrawable mechanicStart, mechanicConnect;

	/**
	 * Create a new instance of LegoBricksFragment, initialized to show the view
	 * at 'index'.
	 * @param index - the index of the fragment
	 */
	public static SettingsFragment newInstance(int index) {
		SettingsFragment f = new SettingsFragment();

		// Supply index input as an argument.
		Bundle args = new Bundle();
		args.putInt("index", index);
		f.setArguments(args);

		return f;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		ctx = getActivity();

		// Inflate the layout for this fragment
		View v = inflater.inflate(R.layout.settings_fragment, container, false);

		editText = (EditText) v.findViewById(R.id.IP_Input);
		editText.setText(Constants.HOST_IP);
		connectBtn = (Button) v.findViewById(R.id.connectBtn);
		disconnectBtn = (Button) v.findViewById(R.id.disconnectBtn);
		connImg = (ImageView) v.findViewById(R.id.connectedImg);
		mechanicImg = (ImageView) v.findViewById(R.id.mechanicAnim);

		if (Lego3DActivity.connector.connected()) {
			mechanicImg.setBackgroundResource(R.drawable.mechanic_succ);
			connImg.setBackgroundResource(R.drawable.connected_img);
		} else {
			connImg.setBackgroundResource(R.drawable.disconnected_img);
			if (Constants.settingsViewAlreadySeen) {
				mechanicImg.setBackgroundResource(R.drawable.mechanic_unsucc);
			} else {
				mechanicImg.setBackgroundResource(R.anim.mechanic_start_anim);
				mechanicStart = (AnimationDrawable) mechanicImg.getBackground();
			}
		}

		if (!Constants.HOST_IP.equalsIgnoreCase("")) {
			editText.setText(Constants.HOST_IP);
		}

		connectBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				String content = editText.getText().toString();

				Matcher matcher = Constants.pattern.matcher(content);
				if (matcher.matches()) {
					Constants.HOST_IP = content;

					if (mechanicStart != null) {
						mechanicStart.stop();
					}

					if (!Lego3DActivity.connector.connected()) {
						Lego3DActivity.connector.start(Constants.HOST_IP);
					} else {
						Toast.makeText(ctx, "Already connected",
								Toast.LENGTH_SHORT).show();
					}
				} else {
					Toast.makeText(ctx, "Invalid IP address",
							Toast.LENGTH_SHORT).show();
				}
			}
		});

		disconnectBtn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Lego3DActivity.connector.sendDisconnect();
				Lego3DActivity.connector.kill();
				updateImage();
			}
		});

		Constants.settingsViewAlreadySeen = true;

		return v;
	}

	@Override
	public void onResume() {
		super.onResume();
		if (mechanicStart != null) {
			mechanicStart.stop();
			mechanicStart.start();
		}
	}
	
	@Override
	public void onStop() {
		
		if (mechanicConnect != null) {
			mechanicConnect.stop();
		}
		
		if (mechanicStart != null) {
			mechanicStart.stop();
		}
		
		super.onStop();
	}

	/**
	 * Updates the image view and the animation according to the connection state.
	 * 
	 */
	public void updateImage() {
		mechanicImg.setBackgroundResource(R.anim.mechanic_conn_anim);
		mechanicImg.requestLayout();
		mechanicConnect = (AnimationDrawable) mechanicImg.getBackground();

		if (Lego3DActivity.connector.connected()) {
			mechanicConnect.addFrame(
					getResources().getDrawable(R.drawable.mechanic_succ), 50);
			connImg.setBackgroundResource(R.drawable.connected_img);
		} else {
			mechanicConnect.addFrame(
					getResources().getDrawable(R.drawable.mechanic_unsucc), 50);
			connImg.setBackgroundResource(R.drawable.disconnected_img);
		}
		mechanicConnect.start();
		connImg.requestLayout();
	}
}
